package vn.aptech.leaveManagementSystem.view.general;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import sun.rmi.log.LogOutputStream;
import vn.aptech.leaveManagementSystem.business.dto.ConfigDatabaseDTO;
import vn.aptech.leaveManagementSystem.dao.DBManager;
import vn.aptech.leaveManagementSystem.view.DemoLgFr;

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;

public class ConfigurationDatabase extends JFrame{

	private JLabel lblLogo;
	private JPanel panel;
	private JTextField txtfServerName;
	private JTextField txtfPort;
	private JTextField txtfLogin;
	private JPasswordField pwdfPass;
	private JButton btnConnect;
	private JButton btnCancel;
	private JLabel lblDatabasename;
	private JTextField txtfDatabaseName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		ConfigurationDatabase config=new ConfigurationDatabase();
		//config.setVisible(true);
	}

	/**
	 * Create the application.
	 */
	public ConfigurationDatabase() {
		setTitle("Configuration Database");
		initGUI();
		initAction();
		testConfigurationDatabase();
	}

	private void initAction() {
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					FileWriter fw=new FileWriter("config/ConfigurationDatabase.lms");
					BufferedWriter bw=new BufferedWriter(fw);
					bw.write(txtfServerName.getText()+"\n");
					bw.write(txtfPort.getText()+"\n");
					bw.write(txtfDatabaseName.getText()+"\n");
					bw.write(txtfLogin.getText()+"\n");
					String pass=new String(pwdfPass.getPassword());
					bw.write(pass+"\n");
					bw.close();
					fw.close();
					getConfigDatabase();
					DBManager db=new DBManager();
					try {
						db.getConn();
						JOptionPane.showMessageDialog(null, "Connect successful!");
					} catch (SQLException e) {
						JOptionPane.showMessageDialog(null, "Cannot connect to database, errors in the parameters you enter. Please re-enter!");
						File f=new File("config/ConfigurationDatabase.lms");
						f.delete();
						return;
					}
					close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	private void initGUI() {
		setBounds(100, 100, 411, 305);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
		
		lblLogo = new JLabel("");
		lblLogo.setIcon(new ImageIcon("backgrounds/Background1.PNG"));
		lblLogo.setBounds(0, 0, 403, 71);
		getContentPane().add(lblLogo);
		
		panel = new JPanel();
		panel.setBounds(0, 71, 403, 153);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblServerName = new JLabel("Server Name:");
		lblServerName.setBounds(10, 11, 99, 14);
		panel.add(lblServerName);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(10, 39, 99, 14);
		panel.add(lblPort);
		
		JLabel lblLogin = new JLabel("Login:");
		lblLogin.setBounds(10, 95, 99, 14);
		panel.add(lblLogin);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(10, 123, 99, 14);
		panel.add(lblPassword);
		
		txtfServerName = new JTextField();
		txtfServerName.setBounds(115, 8, 278, 20);
		panel.add(txtfServerName);
		txtfServerName.setColumns(10);
		
		txtfPort = new JTextField();
		txtfPort.setColumns(10);
		txtfPort.setBounds(115, 36, 278, 20);
		panel.add(txtfPort);
		
		txtfLogin = new JTextField();
		txtfLogin.setColumns(10);
		txtfLogin.setBounds(115, 92, 278, 20);
		panel.add(txtfLogin);
		
		pwdfPass = new JPasswordField();
		pwdfPass.setBounds(115, 120, 278, 20);
		panel.add(pwdfPass);
		
		lblDatabasename = new JLabel("Database Name:");
		lblDatabasename.setBounds(10, 67, 99, 14);
		panel.add(lblDatabasename);
		
		txtfDatabaseName = new JTextField();
		txtfDatabaseName.setColumns(10);
		txtfDatabaseName.setBounds(115, 64, 278, 20);
		panel.add(txtfDatabaseName);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(107, 235, 89, 23);
		getContentPane().add(btnConnect);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnCancel.setBounds(206, 235, 89, 23);
		getContentPane().add(btnCancel);
	}
	
	public void close()
	{
		toBack();
		this.dispose();
		DemoLgFr loginForm=new DemoLgFr();
		loginForm.setVisible(true);
	}
	
	public void testConfigurationDatabase()
	{
		File f=new File("config/ConfigurationDatabase.lms");
		if (f.exists())
		{
			getConfigDatabase();
			DBManager db=new DBManager();
			try {
				db.getConn();
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, "Cannot connect to database, errors in the configuration database. Please reconfiguration!");
				this.setVisible(true);
				return;
			}
			close();
		}
		else
		{
			this.setVisible(true);
		}
	}
	
	public void getConfigDatabase()
	{
		try {
			FileReader fr=new FileReader("config/ConfigurationDatabase.lms");
			BufferedReader br=new BufferedReader(fr);
			
			String serverName=br.readLine();
			String port=br.readLine();
			String databaseName=br.readLine();
			String login=br.readLine();
			String password=br.readLine();
			
			ConfigDatabaseDTO.setServerName(serverName);
			ConfigDatabaseDTO.setPort(port);
			ConfigDatabaseDTO.setDatabaseName(databaseName);
			ConfigDatabaseDTO.setLogin(login);
			ConfigDatabaseDTO.setPassword(password);
			
			br.close();
			fr.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
